<template>
  <div class="layout-padding">
    <splitpanes horizontal>
      <pane size="50">
        <div class="layout-padding-auto layout-padding-view">
          <!--查询表单-->
          <el-form :model="state.queryForm" ref="queryRef" :inline="true" @keyup.enter="getDataList">
            <el-form-item :label="$t('qms.detectionTime')">
              <el-date-picker style="width: 300px"
                              v-model="state.queryForm.checkTime"
                              type="daterange"
                              @change="changeCheckTime"
                              range-separator="To"
                              value-format="YYYY-MM-DD HH:mm:ss"
                              :start-placeholder="$t('iot.startTime')"
                              :end-placeholder="$t('iot.endTime')"/>
            </el-form-item>
            <el-form-item :label="$t('iot.materialCode')">
              <el-input v-model="state.queryForm.materialCode" style="width: 150px" clearable :placeholder="$t('iot.materialCode')"/>
            </el-form-item>
            <el-form-item>
              <el-button icon="search" type="primary"  @click="getDataList">{{ $t('btn.search') }}</el-button>
              <el-button icon="refresh"  plain @click="resetQuery">{{ $t('btn.reset') }}</el-button>
            </el-form-item>
          </el-form>
          <!--查询结果的列表-->
          <el-table :data="state.dataList" highlight-current-row
                    v-loading="state.loading" border style="width: 100%;height: 100%"
                    :cell-style="tableStyle.cellStyle" stripe :header-cell-style="tableStyle.headerCellStyle"
                    @row-click="clickRow"
                    @selection-change="selectionChangeHandle">
            <el-table-column prop="materialCode" :label="$t('iot.materialCode')"/>
            <el-table-column prop="materialName" :label="$t('iot.materialName')"/>
            <el-table-column prop="checkType" :label="$t('qms.detectionClassCode')"/>
            <el-table-column prop="unit" :label="$t('iot.unit')"/>
            <el-table-column prop="qty" :label="$t('iot.qty')"/>
            <el-table-column prop="shiftCode" :label="$t('qms.shiftCode')">
              <template #default="scope">
                <!--根据不同的状态选择不同的颜色的tag-->
                <el-tag effect="plain" :type="scope.row.shiftCode === 'MorningShift' ? 'primary' : 'warning'">
                  {{ scope.row.shiftCode }}
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="shiftName" :label="$t('qms.shiftName')"/>
            <el-table-column prop="status" :label="$t('qms.testResult')">
              <template #default="scope">
                <!--根据不同的状态选择不同的颜色的tag-->
                <el-tag :type="scope.row.status === 'pass' ? 'success' : 'danger'">
                  {{ scope.row.status }}
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="checkDate" :label="$t('qms.dateOfInspection')"/>
            <el-table-column prop="creationTime" width="180" :label="$t('qms.creationTime')"/>
          </el-table>
          <pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" v-bind="state.pagination"/>
        </div>
      </pane>
      <pane size="50">
        <div class="layout-padding-auto layout-padding-view">
          <el-tabs model-value="first">
            <el-tab-pane name="first">
              <template #label>
                <el-icon>
                  <calendar/>
                </el-icon>
                <span class="ml-0.5"> {{ $t('qms.detail') }} </span>
              </template>
            </el-tab-pane>
          </el-tabs>
          <detial ref="locationRef"/>
        </div>
      </pane>
    </splitpanes>
  </div>

</template>

<script setup lang="ts">

import {BasicTableProps, useTable} from "/@/hooks/table";
import detial from "/src/views/qms/qmsFqcCheck/detial.vue";
import {getFqcPageSet} from "/@/api/platform/qms/qmsCheckReport";

// 搜索变量
const queryRef = ref()
const locationRef = ref()

const formatDate = (date: Date): string => {
  console.log(date)
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, '0');
  const day = String(date.getDate()).padStart(2, '0');
  return `${year}-${month}-${day}`;
};

// 仓库表格的配置
const state: BasicTableProps = reactive<BasicTableProps>({
  // queryForm: {"checkStartTime":formatDate(new Date()),"checkEndTime":formatDate(new Date())},
  queryForm: {},
  pageList: getFqcPageSet
})


//  table hook
const {
  tableStyle,
  getDataList,
  sizeChangeHandle,
  currentChangeHandle
} = useTable(state)

// 多选变量
const selectObjs = ref([]) as any
const multiple = ref(true)

// 多选事件
const selectionChangeHandle = (objs: { id: string }[]) => {
  selectObjs.value = objs.map(({id}) => id);
  multiple.value = !objs.length;
};

//时间点击
const changeCheckTime = (data) => {
  state.queryForm.checkStartTime = data[0]
  state.queryForm.checkEndTime = data[1]
}


// 点击行的时候触发
const clickRow = (row: any) => {
  locationRef.value.query(row)
}

// 清空搜索条件
const resetQuery = () => {
  // 清空搜索条件
  // state.queryForm = {"checkStartTime":formatDate(new Date()),"checkEndTime":formatDate(new Date())}
  state.queryForm = {}
  // 清空多选
  selectObjs.value = []
  getDataList(false)

}
</script>
